#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QKeyEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QPen>
#include <QMessageBox>
#include <QPushButton>
#include <QMediaPlayer>
#include <QMediaPlaylist>

MainWindow::MainWindow(QWidget *parent, std::string const& source):
    QMainWindow(parent), ui(new Ui::MainWindow), source(source),
    level(std::move(std::unique_ptr<GameLevel>(new GameLevel(source))))
{
    ui->setupUi(this);

    setFixedSize(1000, 1000);
    restartButton = new QPushButton(this);
    restartButton->move(900, 0);
    restartButton->setParent(this);
    restartButton->resize(100, 100);//大小
    restartButton->setStyleSheet("border-image:url(:/img/img/restart.png);");//插入图片

    connect(restartButton, QPushButton::clicked, this, restartClicked);//联系点击事件与函数

    playList = new QMediaPlaylist();//以下是插入循环播放的音乐
    playList->addMedia(QUrl("qrc:/music/music/bg.wav"));
    playList->setPlaybackMode(QMediaPlaylist::Loop);

    mediaPlayer = new QMediaPlayer;
    mediaPlayer->setPlaylist(playList);
    mediaPlayer->setVolume(50);
    mediaPlayer->play();
}

void MainWindow::paintEvent(QPaintEvent*)
{
    auto const& world = level->world;
    auto const& playerX = level->playerX;
    auto const& playerY = level->playerY;
    auto const& boxes = level->boxes;

    for(int i = 0; i < 20; ++i)//这是20*20的总地图
    {
        for(int j = 0; j < 20; ++j)
        {
            QPainter painter(this);
            if(world[i][j] == GameLevel::WorldTileType::WALL || world[i][j] == GameLevel::WorldTileType::EMPTY)
            {
                painter.drawPixmap(i * 50, j * 50, 50, 50, QPixmap(":/img/img/wall.jpg"));//放入墙的图片，以下同理
            }
            else if(world[i][j] == GameLevel::WorldTileType::FLOOR)
            {
                painter.drawPixmap(i * 50, j * 50, 50, 50, QPixmap(":/img/img/floor.jpg"));
            }
            else if(world[i][j] == GameLevel::WorldTileType::TARGET)
            {
                painter.drawPixmap(i * 50, j * 50, 50, 50, QPixmap(":/img/img/target.jpg"));
            }

            if(i == playerX && j == playerY)
            {
                painter.drawPixmap(i * 50, j * 50, 50, 50, QPixmap(":/img/img/person.png"));
            }

            if(boxes[i][j])
            {
                painter.drawPixmap(i * 50, j * 50, 50, 50, QPixmap(":/img/img/box.jpg"));
            }
        }
    }
}

void MainWindow::keyPressEvent(QKeyEvent * event)
{
    if(event->key() == Qt::Key_W)
    {
        level->movePlayer(0, -1);
    }
    else if(event->key() == Qt::Key_S)
    {
        level->movePlayer(0, 1);
    }
    else if(event->key() == Qt::Key_A)
    {
        level->movePlayer(-1, 0);
    }
    else if(event->key() == Qt::Key_D)
    {
        level->movePlayer(+1, 0);
    }

    if(level->checkSuccess())//用户游戏成功显示胜利的提示
    {
        QMessageBox box(this);
        box.setText("您胜利了！");
        box.setWindowTitle("太好了！");
        box.exec();
    }

    update();

}

void MainWindow::restartClicked()//这是重玩按钮的函数
{
    level = std::unique_ptr<GameLevel>(new GameLevel(source));
    update();
}

MainWindow::~MainWindow()//析构函数，释放
{
    delete ui;
    delete mediaPlayer;
    delete restartButton;
    delete playList;
}
